﻿using System;
using System.Linq;
using System.Web;
using System.IO;

using IdeaNMR.Web.Core;

namespace IdeaNMR.Web
{
    public partial class SpinachMacroDownload : System.Web.UI.Page
    {
        // ALISTAIR: comment added 25/08/2011
        // The entire loading of the macro is performed in the Page_Load
        // method of the ASP.NET lifecycle
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Response.Clear();
                Response.AddHeader("content-type", "application/octet-stream");
                // specify that the response consists of a file attachment, using the current filename
                Response.AddHeader("content-disposition", "attachment;filename=\"SpinachMacro.txt\"");

                Response.Cache.SetCacheability(HttpCacheability.Public);
                Response.Cache.SetSlidingExpiration(true);
                Response.Cache.SetValidUntilExpires(true);
                Response.Cache.VaryByParams["File"] = true;

                string spinachMacroText = Request["SpinachMacroText"];

                // write the script to the resp
                Response.Write(spinachMacroText);

                //  Response.WriteFile(TempFile);
                Response.Flush();
                Response.Close();

            }
            catch (Exception ex)
            {
                ScrewTurn.Wiki.Log.LogEntry("Error writing spinach macro.\n" + ex.Message, ScrewTurn.Wiki.PluginFramework.EntryType.Error, ScrewTurnWikiFacade.Username);
            }
        } // end Page_Load
    } // end SpinachMacroDownload partial class
} // end IdeaNMR.Web